package com.xhj.controller;

import com.xhj.entity.Chapter;
import com.xhj.service.ChapterService;
import com.xhj.util.ResultEntity;
import com.xhj.vo.ChapterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程(EduChapter)表控制层
 *
 * @author makejava
 * @since 2021-02-26 12:09:03
 */
@CrossOrigin
@Api(value="章节管理")
@RestController
@RequestMapping("/EduAdmin/chapter")
public class ChapterController {
    /**
     * 服务对象
     */
    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "章节列表")
    @RequestMapping("list/{courseId}")
    public ResultEntity getChapterList(
            @ApiParam(name = "courseId", value = "课程Id", required = true)
            @PathVariable String courseId){

        List<ChapterVo> chapterVoList = chapterService.chapterList(courseId);
        return ResultEntity.successWithData().data("chapterVoList",chapterVoList);
    }

    @ApiOperation(value = "新增章节")
    @RequestMapping("add")
    public ResultEntity addChapter(
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        chapterService.save(chapter);
        return ResultEntity.successWithData();
    }

    @ApiOperation(value = "根据Id查询章节")
    @RequestMapping("get/{id}")
    public ResultEntity saveChapterById(
            @ApiParam(name = "id", value = "章节Id", required = true)
            @PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return ResultEntity.successWithData().data("chapter", chapter);
    }

    @ApiOperation(value = "根据Id修改章节")
    @RequestMapping("update/{id}")
    public ResultEntity updateChapterById(
            @ApiParam(name = "id", value = "章节Id", required = true)
            @PathVariable String id,
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        boolean resultUpdate = chapterService.updateById(chapter);
        if (resultUpdate) return ResultEntity.successWithoutData();
        return ResultEntity.failedWithMessage("更新失败！");
    }

    @ApiOperation(value = "根据Id删除章节")
    @RequestMapping("delete/{id}")
    public ResultEntity deleteChapterById(
            @ApiParam(name = "id", value = "章节Id", required = true)
            @PathVariable String id){
        boolean result = chapterService.deleteChapterById(id);
        if (!result) return ResultEntity.failedWithMessage("删除失败！");
        return ResultEntity.successWithoutData();
    }
}